package cn.tedu.tmall.admin.mall.controller;


import cn.tedu.tmall.admin.mall.pojo.param.GoodsAddParam;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsListVO;
import cn.tedu.tmall.admin.mall.pojo.vo.GoodsVO;
import cn.tedu.tmall.admin.mall.service.IGoodsService;
import cn.tedu.tmall.common.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = "商品分类管理")
@RequestMapping("mall/goods")
@RestController
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    //增加商品 需要挂到类别上
    //通过类别查询商品列表
    //通过商品id查询商品详情
    @ApiOperation("新增商品")
    @PostMapping("/add-new")
    public JsonResult addNewGoods(@Valid GoodsAddParam param){
        goodsService.addNewGoods(param);
        return JsonResult.ok();
    }
    @ApiOperation("商品列表")
    @PostMapping("/list")
    public JsonResult list(@RequestParam Long categoryId, @RequestParam Integer pageCount, @RequestParam @Range(min = 1,max = 20,message = "每页不能超过20条") Integer pageSize){
        List<GoodsListVO> goodsListVOS=goodsService.goodsList(categoryId,pageCount,pageSize);
        return JsonResult.ok(goodsListVOS);
    }
    @ApiOperation("商品详情")
    @PostMapping("/detail")
    public JsonResult detail(Long id){
        GoodsVO goodsVO =goodsService.selectDetail(id);
        return JsonResult.ok(goodsVO);

    }

}
